Repository navigation
Mock bcryptjs in API tests - #326
Merged
Merged
Conversation
Password hashing used a hard-coded cost of 12 with bcryptjs (pure JS),
so every createUser({ password }) and login in tests paid ~200ms.
The cost now reads PASSWORD_BCRYPT_COST, defaulting to 12, and
vitest.config.js sets it to 4 for the test run.
API ChangesNo changes. |
andrewplummer
approved these changes
Oct 5, 2026
andrewplummer
left a comment
Collaborator
There was a problem hiding this comment.
This is totally fine - approved, however I was thinking to myself maybe mocking bcrypt is the more "correct" way ... either way it's fine
Collaborator
Author
|
yeah i prefer the mocking too, i dont really want an env var for this, let me modify and see if thats possible |
Drops BCRYPT_SALT_PASSES; password.js, .env and vitest.config.js are back to master. Tests now use __mocks__/bcryptjs.js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
services/api/__mocks__/bcryptjs.js: wraps the real bcryptjs and only overridesgenSaltto use cost 4 (bcrypt's minimum).hashandcompareare the real implementations.services/api/src/utils/testing/setup/mocks.js: addsvi.mock('bcryptjs')next to the other package mocks.No production code,
.envor deployment change:password.jsstill hashes at cost 12.Why
setPasswordhashes at cost 12 with bcryptjs (pure JS). EverycreateUser({ password })and every password login in tests paid for a cost-12 hash or compare, which dominated the auth test files.Measurements (local, warm runs)
pnpm test src/routes/auth/password.test.js(20 tests)pnpm test(39 files, 399 tests)Reviewer notes
$2b$04$hashes.BCRYPT_SALT_PASSESenv variable and a fully faked mock; squash-merging leaves only the final mock.